package com.ia.uniweb.test;

import java.nio.ByteBuffer;
import java.util.List;

import me.prettyprint.cassandra.serializers.ByteBufferSerializer;
import me.prettyprint.cassandra.serializers.IntegerSerializer;
import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.beans.HSuperColumn;
import me.prettyprint.hector.api.beans.SuperSlice;
import me.prettyprint.hector.api.factory.HFactory;
import me.prettyprint.hector.api.query.QueryResult;
import me.prettyprint.hector.api.query.SuperSliceQuery;

import com.ia.uniweb.model.Result;

public class IntegerRead {
	
	private static final String keyspace = "University";
	private static final String cassandraServer = "localhost:9160";
	private static final String superColumnFamily = "Results";
	private static StringSerializer stringSerializer = StringSerializer.get();

	
	
	public static void main(String[] args) {
		
		Cluster cluster = HFactory.getOrCreateCluster("TestCluster",
				cassandraServer);

		Keyspace keyspaceOperator = HFactory.createKeyspace(keyspace,
				cluster);
		
		
		SuperSliceQuery<String, String, String, ByteBuffer> squery = HFactory
				.createSuperSliceQuery(keyspaceOperator, StringSerializer.get(),
						StringSerializer.get(), StringSerializer.get(),
						ByteBufferSerializer.get());
		
		squery.setColumnFamily(superColumnFamily);
		squery.setKey("1DS00876");
		squery.setRange("", "", false, 999);

		QueryResult<SuperSlice<String, String, ByteBuffer>> sresults = squery
				.execute();

		List<HSuperColumn<String, String, ByteBuffer>> individualResults = sresults
				.get().getSuperColumns();

		Result[] results = null;
		if (individualResults != null) {
			results = new Result[individualResults.size()];
			int i = 0;
			for (HSuperColumn<String, String, ByteBuffer> hSuperColumn : individualResults) {

				Result result = new Result();

				result.setRegistrationNumber("1DS00876");
				result.setSubject(StringSerializer.get().fromByteBuffer(hSuperColumn.getSubColumnByName("subject").getValue()
						));
				result.setGrade(StringSerializer.get().fromByteBuffer(hSuperColumn.getSubColumnByName("grade").getValue())
						);
				result.setInternalMarks(IntegerSerializer.get().fromByteBuffer(hSuperColumn
						.getSubColumnByName("internalMarks").getValue()));
				result.setPracticalMarks(IntegerSerializer.get().fromByteBuffer(hSuperColumn
						.getSubColumnByName("practicalMarks").getValue()));
				result.setTheoryMarks(IntegerSerializer.get().fromByteBuffer(hSuperColumn
						.getSubColumnByName("theoryMarks").getValue()));
				result.setResult(StringSerializer.get().fromByteBuffer(hSuperColumn.getSubColumnByName("result")
						.getValue()));
				
				System.out.println("Result item : " + result);
				
				results[i++] = result;
			}
		}
		
		
	}

}
